package com.sgb.kjwl.viewmodel.pick;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.pick.PickInfoListEntity;
import com.sgb.kjwl.repository.PickProjectRepertory;
import com.sgb.kjwl.view.ui.fragment.pick.PickAttentionInfoFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PickAttentionInfoModel extends BaseViewModel {

    private final PickAttentionInfoFragment fragment;
    private final PickProjectRepertory repertory;
    private Map map;
    public ObservableField<Integer> page = new ObservableField<>(1);
    private PickSelectEntity mAll;

    public PickAttentionInfoModel(PickAttentionInfoFragment fragment, PickProjectRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
        mAll = new PickSelectEntity();
        mAll.setNo("");
        mAll.setProjectscale("全部");
    }

    public void getList(String cityNo, String typeNo, String areacode) {
        map = new HashMap();
        map.put("limit", 10);
        map.put("page", page.get());
        if (!TextUtils.isEmpty(areacode))
            map.put("areacode", areacode);
        if (!TextUtils.isEmpty(cityNo))
            map.put("city", cityNo);
        if (!TextUtils.isEmpty(typeNo)) {
            if (typeNo.equals("全部")) {
                typeNo = "";
            }
            map.put("tenderType", typeNo);
        }

        DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getTenderInfo(map).enqueue(new ICallBack<BaseEntity<ProListEntity<List<PickInfoListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<PickInfoListEntity>>> data) {
                if (data == null || data.getState() == null) {
                    fragment.showList(new ArrayList<>());
                    return;
                }
                if (null == data.getData()) {
                    fragment.showList(new ArrayList<>());
                    return;
                }
                fragment.showList(data.getData().getList());
            }
        });

    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("tenderNoticeDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getFilterTypeData() {
        map = new HashMap();
        map.put("areacode", repertory.getAreaCode());
        map.put("keyword", "");
        map.put("name", "");
        APIManager.getInstance().getPickAPI().getPickInfoType(map).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTypeDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

            }
        });
    }

    public void getProvinceData() {
        DialogHelper.showProgressDialog2(fragment.getContext(), null, "加载中", 0, true, null,false);
        APIManager.getInstance().getPickAPI().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setProvinceDatas(data.getData());
                }
            }
        });
    }

    public void getAddressCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> children = scrDatum.getChildren();
                if (children != null && children.size() > 0) {
                    List<PickSelectEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : children) {
                        entities.add(new PickSelectEntity(child.getNo(), child.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    fragment.setCityDatas(entities);
                }
            }
        }
    }
}
